/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package getsinanowdata;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;

/**
 *
 * @author ryu
 */
public class HtmlTools {

    private List<String> getHtml(String urlString) {
        try {
            List<String> html = new ArrayList<String>();
            URL url = new URL(urlString);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            try (InputStreamReader isr = new InputStreamReader(conn.getInputStream(), "GBK");
                    BufferedReader br = new BufferedReader(isr)) {
                String temp;
                while ((temp = br.readLine()) != null) {
                    html.add(temp);
                }
            }
            return html;
        } catch (IOException e) {
            return null;
        }
    }

    private List<String> getHtmlDatas(List<String> ids) throws InterruptedException {
        int count = 0;
        List<String> gpdata = new ArrayList<>();
        StringBuilder gpids = new StringBuilder();

        for (String dt : ids) {
            gpids.append(dt);
            System.out.println(dt);
            gpids.append(",");
        }
        gpdata = getHtml("http://hq.sinajs.cn/list=" + gpids.substring(0, gpids.length() - 1));

        return gpdata;
    }

    public static int[] getDate() {
        Calendar cal = Calendar.getInstance();// 取当前日期。
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        int day = cal.get(Calendar.DAY_OF_MONTH);
        int hour = cal.get(Calendar.HOUR_OF_DAY);
        int minute = cal.get(Calendar.MINUTE);
        int second = cal.get(Calendar.SECOND);
        int[] dt = new int[2];
        dt[0] = (year * 10000 + month * 100 + day);
        dt[1] = hour * 10000 + minute * 100 + second;
        return dt;
    }

    public List<data> getGpdatas(List<String> ids) throws InterruptedException {
        List<data> datas = new ArrayList<>();
        int[] gdt = getDate();
        long test = System.currentTimeMillis();
        List<String> outstr = getHtmlDatas(ids);

        for (String temp : outstr) {
            if (temp.length() < 30) {
                continue;
            }
            String[] strarray;
            data dt = new data();
            strarray = temp.split(",");
            dt.getdate = gdt[0];
            dt.gettime = gdt[1];
            dt.gpid = Integer.parseInt(strarray[0].trim().substring(13, 19));
            dt.gpname = (strarray[0].trim().substring(strarray[0].trim().indexOf("=") + 2));
            dt.openprice = Float.parseFloat(strarray[1].trim());
            dt.oldprice = Float.parseFloat(strarray[2].trim());
            dt.price = Float.parseFloat(strarray[3].trim());
            dt.maxprice = Float.parseFloat(strarray[4].trim());
            dt.minprice = Float.parseFloat(strarray[5].trim());
            dt.sumvol = (int) (Long.parseLong(strarray[8].trim()) / 100);
            dt.amount = Float.parseFloat(strarray[9].trim()) / 10000;
            if (dt.price < 0.1 || dt.amount < 0.1 || dt.sumvol < 1) {
                continue;
            }
            dt.avgprice = (float) (Double.parseDouble(strarray[9].trim()) / (Long.parseLong(strarray[8].trim())));
            dt.bv1 = Long.parseLong(strarray[10].trim());
            dt.bp1 = Float.parseFloat(strarray[11].trim());
            dt.bv2 = Long.parseLong(strarray[12].trim());
            dt.bp2 = Float.parseFloat(strarray[13].trim());
            dt.bv3 = Long.parseLong(strarray[14].trim());
            dt.bp3 = Float.parseFloat(strarray[15].trim());
            dt.bv4 = Long.parseLong(strarray[16].trim());
            dt.bp4 = Float.parseFloat(strarray[17].trim());
            dt.bv5 = Long.parseLong(strarray[18].trim());
            dt.bp5 = Float.parseFloat(strarray[19].trim());
            dt.sv1 = Long.parseLong(strarray[20].trim());
            dt.sp1 = Float.parseFloat(strarray[21].trim());
            dt.sv2 = Long.parseLong(strarray[22].trim());
            dt.sp2 = Float.parseFloat(strarray[23].trim());
            dt.sv3 = Long.parseLong(strarray[24].trim());
            dt.sp3 = Float.parseFloat(strarray[25].trim());
            dt.sv4 = Long.parseLong(strarray[26].trim());
            dt.sp4 = Float.parseFloat(strarray[27].trim());
            dt.sv5 = Long.parseLong(strarray[28].trim());
            dt.sp5 = Float.parseFloat(strarray[29].trim());

            String[] strtemp;
            strtemp = strarray[30].trim().split("-");
            dt.gpdate = Integer.parseInt(strtemp[0].trim()) * 10000 + Integer.parseInt(strtemp[1].trim()) * 100 + Integer.parseInt(strtemp[2].trim());
            strtemp = strarray[31].trim().split(":");
            dt.gptime = Integer.parseInt(strtemp[0].trim()) * 10000 + Integer.parseInt(strtemp[1].trim()) * 100 + Integer.parseInt(strtemp[2].trim());
            if (dt.gptime < 91500) {
                continue;
            }
            datas.add(dt);
            //System.out.println(strarray[0].trim().substring(strarray[0].trim().indexOf("=")+2));
            //System.out.println(strarray[0].trim().substring(13,19));
            //System.out.println(i+ "|"+dt.gpid + "|" + dt.gpname + "|" + dt.price + "|" + dt.sumvol + "|" + dt.amount + "|" + dt.gpdate + "|" + dt.gptime);
            //i++;
        }
         System.out.println("Html时间:" + (System.currentTimeMillis() - test));
        System.out.println("datas:" + datas.size());
        return datas;
    }

}
